package cn.caijiapeng.spt.config;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.template.Engine;
import com.jfinal.template.source.ClassPathSourceFactory;

import cn.caijiapeng.spt.ctrl.DataCtrl;
import cn.caijiapeng.spt.ctrl.IndexCtrl;

public class WebConfig extends JFinalConfig{

	@Override
	public void configConstant(Constants me) {
		PropKit.use("application.properties");
		me.setDevMode(PropKit.getBoolean("devMode", false));
		me.setMaxPostSize(1024 * 100);
	}

	@Override
	public void configEngine(Engine arg0) {
		
	}

	@Override
	public void configHandler(Handlers arg0) {
		
		
	}

	@Override
	public void configInterceptor(Interceptors arg0) {
		
	}

	@Override
	public void configPlugin(Plugins me) {
		String url = PropKit.get("database.jdbcUrl").trim();
		String username = PropKit.get("database.username").trim();
		String password = PropKit.get("database.password").trim();
		String driverClass = PropKit.get("database.driverClass").trim();
		DruidPlugin druidPlugin = new DruidPlugin(url, username, password, driverClass);
		me.add(druidPlugin);
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		arp.getEngine().setSourceFactory(new ClassPathSourceFactory());
		arp.addSqlTemplate("sql/all.sql");
		MappingKit.addMapping(arp);
		arp.setShowSql(PropKit.getBoolean("showSql", false));
		arp.setDialect(new MysqlDialect());
		me.add(arp);
	}

	@Override
	public void configRoute(Routes me) {
		me.setBaseViewPath("/page/admin");
		me.add("/", IndexCtrl.class);
		me.add(new AdminRoute());
		me.add("/data", DataCtrl.class);
	}

}
